package com.cip.master.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.velocity.app.VelocityEngine;

import com.cip.common.util.CommonUtil;
import com.cip.common.util.GridUtil;

import com.cip.master.dao.AcademicDAO;
import com.cip.master.model.Academic;
import com.cip.master.service.AcademicService;

public class AcademicServiceImpl implements AcademicService {
	
	private AcademicDAO academicDAO;
	private VelocityEngine velocityEngine;
	
	
	public String getAcademicGridList(Locale locale)throws Exception {
		
		String gridSource = ""; 
		
		try {
			List<Academic> academicList = getAllAcademicList();
			
			List<Map<String,Object>> academicArrayList = new ArrayList<Map<String,Object>>();
			
			for (Academic academic : academicList) {
				Map<String,Object> academicObj = new HashMap<String, Object>();
				academicObj.put("id", "<a href='/editAcademic?method=showEditAcademic&academic.id="+academic.getId()+"' style='text-decoration:underline;color:blue;'>"+academic.getId()+"</a>");
				academicObj.put("fromDate", academic.getFromDate());
				academicObj.put("toDate", academic.getToDate());
				academicObj.put("status", academic.getStatus());
				
				academicArrayList.add(academicObj);			
			}
			
			gridSource = generateScriptForProcess(velocityEngine, academicArrayList, locale);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return gridSource;
	}
	
	/**
	 * Set into context of grid column names and field names and it attributes
	 * 
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public static String generateScriptForProcess(VelocityEngine velocityEngine, List<Map<String, Object>> academicArrayList, Locale locale) throws Exception {
		
		return GridUtil.generateScriptForProcess(velocityEngine, academicArrayList, createAcademicGridColumnMapping(locale));
	}
	
	/**
	 * Add needed column and it mapping for my instances
	 * 
	 * @param context
	 */
	private static Map<String, Object> createAcademicGridColumnMapping(Locale locale) {
		Map<String, Object> context = new HashMap<String, Object>();
		List<Map<String, Object>> fieldNameList = new ArrayList<Map<String, Object>>();
		//ResourceBundle appResourceBundle = ResourceBundle.getBundle(Constants.BUNDLE_KEY ,locale);
		//context.put("title", appResourceBundle.getString("process.myInstances"));
		context.put("title", "Academic List");
		context.put("gridId", "MY_AVADEMIC");
		context.put("needTreeStructure", true);
		//context.put("dynamicGridWidth", "300px");
		//context.put("dynamicGridHeight", "580px");
		String columnNames = "['Id','From Date','To Date','Status']";
        CommonUtil.createFieldNameList(fieldNameList, "id", "100", "center", "","false");
        CommonUtil.createFieldNameList(fieldNameList, "fromDate", "100", "left","", "false");
        CommonUtil.createFieldNameList(fieldNameList, "toDate", "100","left", "", "false");
        CommonUtil.createFieldNameList(fieldNameList, "status", "100", "center", "", "false");

        context.put("columnNames", columnNames);
        context.put("noOfRecords", "10");
		context.put("fieldNameList", fieldNameList);
		context.put("gridHeading", "true");
		
		return context;
	}
	
	
	public List<Academic> getAllAcademicList() throws Exception {
		return academicDAO.getAllAcademicList();
	}

	public Academic getAcademicById(int id) throws Exception {
		return academicDAO.getAcademicById(id);
	}

	public Academic saveOrUpdateAcademic(Academic academic) throws Exception {		
		return academicDAO.saveOrUpdateAcademic(academic);
	}

	public Academic editAcademic(Academic academic) throws Exception {
		return academicDAO.editAcademic(academic);
	}
	
	public Academic getAcademicByYear(String year) throws Exception{
		return academicDAO.getAcademicByYear(year);
	}

	public AcademicDAO getAcademicDAO() {
		return academicDAO;
	}

	public void setAcademicDAO(AcademicDAO academicDAO) {
		this.academicDAO = academicDAO;
	}

	/**
	 * Getter and Setter
	 */
	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}
	
	
	
}
